package atom.core2.j2ee.util;

import java.net.URLEncoder;



public class UrlUtil
{
	private StringBuffer url = null;
	private String urlEncode;
	public static String getUrlPrefix(String url)
	{
		int pos = url.indexOf("?");
		if (pos==-1)
			return url;
		else
			return url.substring(0,pos);
	}
	public static String getUrlSuffix(String url)
	{
		int pos = url.indexOf("?");
		if (pos==-1)
			return "";
		else
			return url.substring(pos+1);
	}
	public void setPrefix(String urlPrefix,String urlEncode)
	{
		url = new StringBuffer();
		int pos = urlPrefix.indexOf("?");
		url.append(urlPrefix);
		//如果无，则必须加? www.123.com/aa.do
		if (pos==-1)
			url.append("?");
		//有问号，还有参数  www.123.com/aa.do?
		else if (pos!=urlPrefix.getBytes().length -1)
			url.append("&");
		
		//如果有? 号，又是最后的参数，原样调用；www.123.com/aa.do?method=1
		
	}
	public void addParm(String paramName,String paramValue)
	{
		url.append(paramName);
		url.append("=");
		try
		{
		url.append(URLEncoder.encode(paramValue,urlEncode));
		}
		catch(Exception e)
		{
			url.append(URLEncoder.encode(paramValue));
		}
		url.append("&");
	}

	public String getUrl()
	{
		return url.toString();
	}
	public void setUrl(StringBuffer url)
	{
		this.url = url;
	}
}
